/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
osThreadId led_TaskHandle;
osThreadId buzz_TaskHandle;
osThreadId mainHandle;
osThreadId core_taskHandle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void led(void const * argument);
void buzzTask(void const * argument);
void rtos_main(void const * argument);
void core(void const * argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
  *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
  *ppxIdleTaskStackBuffer = &xIdleStack[0];
  *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  /* place for user code */
}
/* USER CODE END GET_IDLE_TASK_MEMORY */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* definition and creation of led_Task */
  osThreadDef(led_Task, led, osPriorityNormal, 0, 64);
  led_TaskHandle = osThreadCreate(osThread(led_Task), NULL);

  /* definition and creation of buzz_Task */
  osThreadDef(buzz_Task, buzzTask, osPriorityRealtime, 0, 64);
  buzz_TaskHandle = osThreadCreate(osThread(buzz_Task), NULL);

  /* definition and creation of main */
  osThreadDef(main, rtos_main, osPriorityNormal, 0, 128);
  mainHandle = osThreadCreate(osThread(main), NULL);

  /* definition and creation of core_task */
  osThreadDef(core_task, core, osPriorityHigh, 0, 128);
  core_taskHandle = osThreadCreate(osThread(core_task), NULL);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

}

/* USER CODE BEGIN Header_led */
/**
  * @brief  Function implementing the led_Task thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_led */
void led(void const * argument)
{
  /* USER CODE BEGIN led */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END led */
}

/* USER CODE BEGIN Header_buzzTask */
/**
* @brief Function implementing the buzz_Task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_buzzTask */
void buzzTask(void const * argument)
{
  /* USER CODE BEGIN buzzTask */
  /* Infinite loop */
  for(;;)
  {
    buzz_Handle();
  }
  /* USER CODE END buzzTask */
}

/* USER CODE BEGIN Header_rtos_main */
/**
* @brief Function implementing the main thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_rtos_main */
void rtos_main(void const * argument)
{
  /* USER CODE BEGIN rtos_main */
//	buzz_didi(2);
  /* Infinite loop */
  for(;;)
  {
		/* 核心计算 */
		my_math_update();
		
		/* 显示 */
		oled_display();
		
    osDelay(1);
  }
  /* USER CODE END rtos_main */
}

/* USER CODE BEGIN Header_core */
/**
* @brief Function implementing the core_task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_core */
void core(void const * argument)
{
  /* USER CODE BEGIN core */
	uint32_t tick;
  /* Infinite loop */
  for(;;)
  {
		osDelay(1);
		
		if (RATE_DO_EXECUTE(RATE_1000_HZ, tick)) {
			adc_driver_update();
		}
		
		if (RATE_DO_EXECUTE(RATE_50_HZ, tick)) {
			key_update();
//			laser_tx_cmd(la_voltage);
//			laser_write_data(get_sign_high_num, 2);
		}
		
		if (RATE_DO_EXECUTE(RATE_10_HZ, tick)) {
			laser_write_data(get_sign_high_num, 2);
		}
		
		tick++;
  }
  /* USER CODE END core */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

